package com.sanyue.codingsettlement.strategy;

import com.sanyue.codingsettlement.common.ErrorCode;
import com.sanyue.codingsettlement.exception.BusinessException;
import com.sanyue.codingsettlement.model.entity.App;
import com.sanyue.codingsettlement.model.entity.UserAnswer;
import com.sanyue.codingsettlement.model.enums.AppTypeStatusEnum;
import com.sanyue.codingsettlement.model.enums.ScoringStrategyStatusEnum;
import org.springframework.stereotype.Service;

import java.lang.annotation.Annotation;
import java.util.List;

/**
 * 评分策略执行器，用于区分使用哪种策略
 * @author: Guan
 **/
@Service
public class ScoringStrategyExecutor{

    List<ScoringStrategy> scoringStrategyList;


    public UserAnswer doScore(App app, List<String> choiceList) {
        Integer appType = app.getAppType();
        Integer scoringStrategy = app.getScoringStrategy();
        if(AppTypeStatusEnum.getEnumByValue(appType) == null || ScoringStrategyStatusEnum.getEnumByValue(scoringStrategy) == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"策略有误");
        }
        for (ScoringStrategy strategy : scoringStrategyList) {
            //该类是否有自定义注解
            if(strategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class)){
                ScoringStrategyConfig annotation = scoringStrategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                int strategyAppType = annotation.appType();
                int strategyScoringStrategy = annotation.scoringStrategy();
                if(strategyAppType == appType && strategyScoringStrategy == scoringStrategy){
                    return strategy.doScore(app,choiceList);
                }
            }

        }
        throw new BusinessException(ErrorCode.PARAMS_ERROR,"策略不存在");
    }
}
